package cn.imu.appdevexperiment2020.Application.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import cn.imu.appdevexperiment2020.Application.api.ApiClient;
import cn.imu.appdevexperiment2020.Application.bean.user.RegisterReceiveBean;
import cn.imu.appdevexperiment2020.Application.bean.user.RegisterSendBean;
import cn.imu.appdevexperiment2020.Application.util.AuthUtil;
import cn.imu.appdevexperiment2020.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity{
    public static final String TAG="Register";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Button registerButton = findViewById(R.id.register);
        EditText userNameEt =findViewById(R.id.username);
        EditText passwordEt =findViewById(R.id.password1);
        EditText nicknameEt =findViewById(R.id.nickName);
        EditText classIdEt =findViewById(R.id.classId);
        EditText emailEt =findViewById(R.id.email);
        EditText phoneNumberEt =findViewById(R.id.phoneNumber);
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioGroup sexGroup=findViewById(R.id.gender);
                RadioGroup statusGroup=findViewById(R.id.status);
                RadioButton sexButton=findViewById(sexGroup.getCheckedRadioButtonId());
                RadioButton statusButton=findViewById(statusGroup.getCheckedRadioButtonId());
                String username= userNameEt.getText().toString();
                String password= passwordEt.getText().toString();
                String nickname= nicknameEt.getText().toString();
                String classId= classIdEt.getText().toString();
                String email= emailEt.getText().toString();
                String phoneNumber= phoneNumberEt.getText().toString();
                String sex = sexButton.getTag().toString();
                String status = statusButton.getTag().toString();
                RegisterSendBean registerSendBean = new RegisterSendBean();
                registerSendBean.setUserName(username);
                registerSendBean.setPassword(password);
                registerSendBean.setNickName(nickname);
                registerSendBean.setClassesId(classId);
                registerSendBean.setEmail(email);
                registerSendBean.setPhonenumber(phoneNumber);
                registerSendBean.setSex(sex);
                registerSendBean.setStatus(status);
                Call<RegisterReceiveBean> call= ApiClient.getInstance(getApplicationContext()).getReturnData(AuthUtil.GetAuth(),registerSendBean);
                call.enqueue(new Callback<RegisterReceiveBean>() {
                    @Override
                    public void onResponse(Call<RegisterReceiveBean> call, Response<RegisterReceiveBean> response) {
                        if(null!=response.body())
                        {
                            if(200==response.body().getCode())
                            {
                                Log.d(TAG, response.body().getMsg());
                                Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else{
                                Toast.makeText(getApplicationContext(),response.body().getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }else
                            Toast.makeText(getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFailure(Call<RegisterReceiveBean> call, Throwable t) {
                        Toast.makeText(getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}


